package com.tm.test;
//package com.example.demo.utils;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiRobotSendRequest;
import com.dingtalk.api.response.OapiRobotSendResponse;
import com.taobao.api.ApiException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.springframework.stereotype.Component;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author qwa
 * @Description
 * @date 18/4/2022
 */
@Component("dingDingUtils")
@Slf4j
public class RobatDemo {
    public String needSendInfo =
            "Hi good afternoon";


    public void sendDingDing(String url) throws ApiException, UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException {
        //https://oapi.dingtalk.com/robot/send?access_token=XXXXXX&timestamp=XXX&sign=XXX
        //如果创建机器人的时候选择了加密，就需要getSign(XXX) : xxx指的是加签的时候生成的字符串。另外可以点击 密钥如上，签名方法请参考 说明文档
        String sign = getSign("SECeb052111897e3369ccd423c7d177f38cfd65c49ccabf2df9a30083c6f69ca");
        url = url + sign ;
        DingTalkClient client = new DefaultDingTalkClient(url);
        OapiRobotSendRequest request = new OapiRobotSendRequest();
        request.setMsgtype("text");
        OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
        text.setContent(needSendInfo);
        request.setText(text);
        //就算不艾特某个人，也加上，setIsAtAll(false)
        OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
        // isAtAll类型如果不为Boolean，请升级至最新SDK
        at.setIsAtAll(false);
        request.setAt(at);
        OapiRobotSendResponse response = client.execute(request);
    }

    public String getSign(String secret) throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException {
        Long timestamp = System.currentTimeMillis();
        String stringToSign = timestamp + "\n" + secret;
        Mac mac = Mac.getInstance("HmacSHA256");
        mac.init(new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA256"));
        byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8"));
        String sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)),"UTF-8");
        //https://oapi.dingtalk.com/robot/send?access_token=XXXXXX&timestamp=XXX&sign=XXX
        String signUrl = "&timestamp="+timestamp+"&sign="+sign ;
        return signUrl ;
    }

    public static void main(String[] args) throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException, ApiException {

        String url = "https://oapi.dingtalk.com/robot/send?access_token=8dbf7ebaa5e08ae2fb42cc6413af0235588fc0e0811bd2e95529128c0ba3a842";
//        DingDingUtils dingDingUtils = null;
       // RobatDemo robatDemo = new RobatDemo();
//        System.out.println("zai ganam");
        //robatDemo.sendDingDing(url);


        RobatDemo robatDemo1 = new RobatDemo();
        String actual = "Hi Have a great weekend, everyone\n" +
                "\n";
        robatDemo1.needSendInfo = new SimpleDateFormat("yyyy-MM-dd").format(new Date())+"\niOS"+"运行时检测到错误信息：请查收\n"+actual;
        robatDemo1.sendDingDing(url);


    }




}

